<?php
	/*
	 * File: print_api.php
	 * Last modification: 22.07.2011
	 * Author(s): kivio.wanderley
	 * 
	 */
?>

<?php /** CENTER PHP CODE */
	
	/**
	 * Creates and show a button.
	 * @param (string) $p_name Description shown on the button;
	 * @param (string) $p_id Button Id;
	 * @param (bool) $p_disabled Disabled attribute;
	 */
	function print_submit_button( $p_name, $p_id = "", $p_disabled = false, $p_command = "" ) {
		if( $p_disabled == true ) { $t_disabled = "disabled"; } else { $t_disabled = ""; }
		if($p_id == "") $p_id = $p_name;
		if( lang_get($p_name) ) $p_name = lang_get($p_name);		
		
		echo '<input id="'.$p_id.'" type="submit" '.$t_disabled.' class="button" value="'.$p_name.'" '.$p_command.' />' , "\n";
	}

	/**
	 * Creates and show a button with a JS function and no submit.
	 * @param (string) $p_name Description shown on the button;
	 * @param (string) $p_id Button Id;
	 * @param (bool) $p_disabled Disabled attribute;
	 * @param (string) $p_js Javascript function on mouse click;
	 */
	function print_button( $p_name, $p_id = "", $p_disabled = false, $p_command = "" ) {
		if( $p_disabled == true ) { $t_disabled = "disabled"; } else { $t_disabled = ""; }
		if($p_id == "") $p_id = $p_name;
		if( lang_get($p_name) ) $p_name = lang_get($p_name);
		
		echo '<input id="'.$p_id.'" type="button" '.$t_disabled.' class="button" value="'.$p_name.'" onclick="'.$p_command.'" />' , "\n";
	}
 
	/**
	 * Redirect to another page.
	 * @param (string) $p_url Name of the page file;
	 * @param (bool) $p_die Terminate the execution of the script immediately if true;
	 * @return True if its successful or an erro.
	 */
	function print_header_redirect( $p_url, $p_die = true ) {
		$t_url = $p_url;

		# Don't send more headers if they have already been sent (guideweb)
		if ( ! headers_sent() ) {
			header( 'Content-Type: text/html; charset="iso-8859-1"' );
			header( 'Location: '.$t_url );

		} else {
			trigger_error("ERRO REDIRECTING PAGE", E_CORE_ERROR);
		}

		if ( $p_die ) {
			die; # additional output can cause problems so let's just stop output here
		}

		return true;
	}
	

	/**
	 * Print an input text field.
	 * @param (string) $p_name Fields name for post and get
	 * @param (string) $p_id Fields ID for page indentification
	 * @param (string) $p_value Initial value
	 * @param (int) $p_size Size of the fileds box
	 * @param (int) $p_maxlength Max number of characters
	 * @param (bool) $p_required Field need to be filled for form action
	 * @param (string) $p_class Class of the field
	 * @param (bool) $p_disabled True for disabled and false for enabled
	 */
	function print_text_field($p_name, $p_id, $p_value = "", $p_size = 25, $p_maxlength = 25, 
							  $p_required = false, $p_class = "text", $p_disabled = false) 
	{
		$req_bar = '';
		
		if( $p_disabled == true ) { $t_disabled = "disabled"; } else { $t_disabled = ""; }
		
		if($p_required == true) { $p_class = "required"; $req_bar = '<tt class="reqsymbol">| </tt>'; } 
		
		if($p_id == "") $p_id = $p_name;

		echo '<input 
					type="text" name="'.$p_name.'" id="'.$p_id.'"
					class="'.$p_class.'" value="'.$p_value.'" 
				 	size="'.$p_size.'" maxlength="'.$p_maxlength.'" '.$t_disabled.' />';
		
		echo $req_bar;
	}
	
	/**
	 * Print a inputs radio type field; When more than one is needed the param p_value has to be an array
	 * like array(KEY=>VALUE) on KEY for description and VALUE for value of the field.
	 * @param (string) $p_name Name and Id of the field;
	 * @param (string) $p_class Class of the input tag;
	 * @param (string) $p_value Value text to show;
	 * @param (string) $p_string Text besides the radio;
	 * @param (string) $p_onkeypress Javascript function when key is pressed;
	 * @param (bool) $p_disabled Disabled attribute;
	 */
	function print_radio_field( $p_name, $p_id, $p_value, $p_string = "",
								$p_class = "", $p_disabled = false) 
	{
		if( $p_disabled == true ) { $t_disabled = "disabled"; } else { $t_disabled = ""; }
		
		if($p_id == "") $p_id = $p_name;
		
		$t_first = false; //Let the first one be checked
		if( is_array($p_value)) {
			foreach( $p_value as $key => $value ) {
				if($t_first == false) { $t_first = true; $t_checked = "checked"; } else { $t_checked = ''; }
				echo '<input 
							type="radio" id="'.$p_name.'" 
							name="'.$p_name.'" class="'.$p_class.'" value="'.$value.'" 
						 	'.$t_checked.' '.$t_disabled.' />';
				if( !lang_get($key) ) 	echo $key;
				else					echo lang_get($key); //Text besides the radio
			}
			
		} else { # In case no need more than one radio input
			echo '<input 
						type="radio" id="'.$p_name.'" 
						name="'.$p_name.'" class="'.$p_class.'" value="'.$p_value.'" 
					 	'.$t_checked.' '.$t_disabled.' />';
			if( !lang_get($p_string) ) 	echo $p_string;
			else						echo lang_get($p_string); //Text besides the radio
		}
	}
	
	/**
	 * Print an input checkbox field.
	 * @param (string) $p_name Fields name for post and get
	 * @param (string) $p_id Fields ID for page indentification
	 * @param (string) $p_value Field value for post and get
	 * @param (int) $p_state True for checked and false for unchecked
	 * @param (int) $p_description String describing the field
	 * @param (bool) $p_required Field need to be filled for form action
	 * @param (string) $p_class Class of the field
	 * @param (bool) $p_disabled True for disabled and false for enabled
	 */
	function print_checkbox_field( $p_name, $p_id, $p_value, $p_state = false, $p_description = "", 
								   $p_required = false, $p_class = "", $p_disabled = false) 
	{
		if( $p_disabled == true ) { $t_disabled = "disabled"; } else { $t_disabled = ""; }
		if( $p_state == true ) { $t_state = "checked"; } else { $t_state = ""; }
		if($p_id == "") $p_id = $p_name;
		
		# If its a required field set onblur javascript function else its a param for onblur
		if(is_string($p_required)) { $t_onblur = $p_required; }
		else if($p_required == true) { $t_onblur = "js_validate_text_empty(this)"; 
			$req_bar = '<tt class="reqsymbol">| </tt>'; 
		}

		echo '<input 
					type="checkbox" id="'.$p_id.'"
					name="'.$p_name.'" class="'.$p_class.'" value="'.$p_value.'"
					'.$t_state.' '.$t_disabled.'
					onkeypress="'.$p_onkeypress.'" 
					onblur="'.$t_onblur.'" />'. $p_description;
		
		echo $req_bar;
	}
	
	/**
	 * Prints two list to transfer itens for submit of the second list items;
	 * Generates the post for 'list_two_name_list' as array; Set js function onsubmit 
	 * on 'form' for all select.
	 * @param (string) $p_list_one_name Name for first list and used on its Id;
	 * @param (string) $p_list_two_name Name for second list and used on its Id;
	 * @param (string) $p_type Type of the list, if gets custom;
	 * @param (array) $p_items Array of items if second its a custom one;
	 */
	function print_list_transfer_field( $p_list_one_name, $p_list_two_name, $p_type, $p_items = null) 
	{
		$t_list_one_trans_name = lang_get($p_list_one_name);
		$t_list_two_trans_name = lang_get($p_list_two_name);
		
		PRINT '<table><!-- List Tranfer -->';
		PRINT '<tr><td>';
			
			# First list
			
			PRINT '<table>';
			PRINT '<tr><td><label>'. $t_list_one_trans_name . ":" .'</label></td></tr>';
			PRINT '<tr>';
				if( $p_type == 'class' || $p_type == 'enroll_class' ) { 
					PRINT '<td>';
						print_class_option_list("", $p_list_one_name, "", 'multiple SIZE=5 style="width: 300px"');
					PRINT '</td>'; 
				
				} else if( $p_type == 'student' || $p_type == 'enroll_student' ) { 
					PRINT '<td>';
						print_student_option_list("", $p_list_one_name, "", 'multiple SIZE=5 style="width: 300px"');
					PRINT '</td>'; 
					
				} else {
					//Nothing yet
					
				}
			PRINT '</tr>';
			PRINT '<tr>';
				PRINT '<td><input id="to_right" class="button" type="button" value=">>" style="float: right" /></td>';
			PRINT '</tr>';
			PRINT '</table><br />';
			
		PRINT '</td><td>';
				PRINT '<hr width=1 size=150%>'; //Vertical line that separates the lists
		PRINT '</td><td>';
			
			# Second list
			
			PRINT '<table>';
			PRINT '<tr><td><label>'. $t_list_two_trans_name . ":" .'</label></td></tr>';
			PRINT '<tr>';
				if( $p_type == 'enroll_class' ) {
					PRINT '<td>';
					PRINT '<select id="'.$p_list_two_name.'" name="'.$p_list_two_name.'_list[]" multiple size=5 style="width: 300px" >';
						foreach ($p_items as $key => $value) {
							echo '<option value='.$p_items[$key]['tur_id'].' >'.$p_items[$key]['tur_name'].'</option>';
						}
					PRINT '</select>'; 
					PRINT '</td>'; 
				} else if( $p_type == 'enroll_student' ) {
					PRINT '<td>';
					PRINT '<select id="'.$p_list_two_name.'" name="'.$p_list_two_name.'_list[]" multiple size=5 style="width: 300px" >';
						foreach ($p_items as $key => $value) {
							echo '<option value='.$p_items[$key]['al_id'].' >'.$p_items[$key]['al_first_name'].' '.$p_items[$key]['al_last_name'].'</option>';
						}
					PRINT '</select>'; 
					PRINT '</td>'; 	
				} else {
					/* Empty list */
					PRINT '<td><select id="'.$p_list_two_name.'" name="'.$p_list_two_name.'_list[]" multiple size=5 style="width: 300px">';
					PRINT '</select></td>';
				}
			PRINT '</tr>';
			PRINT '<tr>';
				PRINT '<td><input id="to_left" class="button" type="button" value="<<" style="float: left" /></td>';
			PRINT '</tr>';
			PRINT '</table><br />';
			
		PRINT '</td></tr>';
		PRINT '</table>';

	}
	
	/* ========================================
	 * Option lists
	 * ========================================
	 */
	
	/**
	 * Prints a select option list of days on a week.
	 * @param $p_value Initial selected value
	 * @param (string) $p_name Fields name for post and get
	 * @param (string) $p_id Fields ID for page identification
	 * @param (string) $p_attributes Other attributes for the field
	 */
	function print_days_option_list( $p_value = null, $p_name = "days_option_list", $p_id = "", $p_attributes = "" ) {
		if($p_id == "") $p_id = $p_name;
		
		echo '<select name="'.$p_name.'" id="'.$p_id.'" '.$p_attributes.'>';

			$t_days_array = array(SATURDAY, SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY);

			foreach( $t_days_array as $val ) {
				if( $val == $p_value ) {
					echo '<option selected value="'.$val.'">'. lang_get($val) .'</option>';	
				} else {
					echo '<option value="'.$val.'">'. lang_get($val) .'</option>';	
				}
			}
					
		echo '</select>';
	}
	
	/**
	 * Prints a select option list of heard about default options.
	 * @param $p_value Initial selected value
	 * @param (string) $p_name Fields name for post and get
	 * @param (string) $p_id Fields ID for page identification
	 * @param (string) $p_attributes Other attributes for the field
	 */
	function print_heard_about_option_list( $p_value = null, $p_name = "heard_about_option_list", $p_id = "", $p_attributes = "" ) {
		if($p_id == "") $p_id = $p_name;
		
		echo '<select name="'.$p_name.'" id="'.$p_id.'" '.$p_attributes.'>';

			$t_heard_about_array = array(FRIENDS, POSTER, BANNER, WEB_PAGE, BROCHURE, TELEVISION);

			foreach( $t_heard_about_array as $val ) {
				if( $val == $p_value ) {
					echo '<option selected value="'.$val.'">'. lang_get($val) .'</option>';	
				} else {
					echo '<option value="'.$val.'">'. lang_get($val) .'</option>';	
				}
			}
					
		echo '</select>';
	}
	
	/**
	 * Prints a select option list of marital default status options.
	 * @param $p_value Initial selected value
	 * @param (string) $p_name Fields name for post and get
	 * @param (string) $p_id Fields ID for page identification
	 * @param (string) $p_attributes Other attributes for the field
	 */
	function print_marital_status_option_list( $p_value = null, $p_name = "marital_status_option_list", $p_id = "", $p_attributes = "" ) {
		if($p_id == "") $p_id = $p_name;
		
		echo '<select name="'.$p_name.'" id="'.$p_id.'" '.$p_attributes.'>';

			$t_marital_status_array = array(SINGLE, MARRIED, SEPARATED, DIVORCED, WIDOWED);

			foreach( $t_marital_status_array as $val ) {
				if( $val == $p_value ) {
					echo '<option selected value="'.$val.'">'. lang_get($val) .'</option>';	
				} else {
					echo '<option value="'.$val.'">'. lang_get($val) .'</option>';	
				}
			}
					
		echo '</select>';
	}
	
	/**
	 * Prints a select option list of the default types used on site.
	 * @param $p_value Initial selected value
	 * @param (string) $p_name Fields name for post and get
	 * @param (string) $p_id Fields ID for page identification
	 * @param (string) $p_attributes Other attributes for the field
	 */
	function print_type_option_list( $p_value = null, $p_name = "type_option_list", $p_id = "" ) {
		if($p_id == "") $p_id = $p_name;
		
		echo '<select name="'.$p_name.'" id="'.$p_id.'" class="type_list">';
		
			$t_types_array = array(PEOPLE, TEACHER, STUDENT, CLASS_);

			foreach( $t_types_array as $val ) {
				if( $val == $p_value ) {
					echo '<option selected value="'.$val.'">'. lang_get($val) .'</option>';	
				} else {
					echo '<option value="'.$val.'">'. lang_get($val) .'</option>';	
				}
			}
					
		echo '</select>';
	}
	
	/**
	 * Prints a select option list of the access levels.
	 * @param $p_value Initial selected value
	 * @param (string) $p_name Fields name for post and get
	 * @param (string) $p_id Fields ID for page identification
	 * @param (string) $p_attributes Other attributes for the field
	 */
	function print_access_option_list( $p_value = null, $p_name = "access_option_list", $p_id = "", $p_attributes = "" ) {
		if($p_id == "") $p_id = $p_name;
		
		echo '<select name="'.$p_name.'" id="'.$p_id.'" '.$p_attributes.'>';

			$t_days_array = array(ADMINISTRATOR, EMPLOYEE, VISITOR);

			foreach( $t_days_array as $val ) {
				if( $val == $p_value ) {
					echo '<option selected value="'.$val.'">'. lang_get($val) .'</option>';	
				} else {
					echo '<option value="'.$val.'">'. lang_get($val) .'</option>';	
				}
			}
					
		echo '</select>';
	}
	
	/**
	 * Prints a select option list of people selected on database.
	 * @param $p_value Initial selected value
	 * @param (string) $p_name Fields name for post and get
	 * @param (string) $p_id Fields ID for page identification
	 * @param (string) $p_attributes Other attributes for the field
	 */
	function print_people_option_list( $p_value = null, $p_name = "people_option_list", $p_id = "", $p_attributes = "" ) {
		if($p_id == "") $p_id = $p_name;
		
		echo '<select name="'.$p_name.'" id="'.$p_id.'" '.$p_attributes.'>';
			
//			$t_query = "SELECT p.p_id AS id, p.p_nome AS nome, p.p_sobrenome AS sobrenome
//						FROM tb_pessoa p, tb_aluno al, tb_professor prof
//						WHERE p.p_id != al.fk_p_id AND p.p_id != prof.fk_p_id
//						GROUP BY p.p_id, p.p_nome, p.p_sobrenome;";
			$t_query = "SELECT id AS id, nome AS nome, sobrenome AS sobrenome
						FROM tb_usuario;";	
			$t_row = datab_query($t_query);
			
			foreach( $t_row as $row ) {
				if( $row['id'] == $p_value ) {
					echo '<option selected value="'.$row['id'].'">'.$row['nome'].' '.$row['sobrenome'].'</option>';
				} else {
					echo '<option value="'.$row['id'].'">'.$row['nome'].' '.$row['sobrenome'].'</option>';	
				}
					
			}
					
		echo '</select>';
	}
	
	/**
	 * Prints a select option list of users selected on database.
	 * @param $p_value Initial selected value
	 * @param (string) $p_name Fields name for post and get
	 * @param (string) $p_id Fields ID for page identification
	 * @param (string) $p_attributes Other attributes for the field
	 */
	function print_user_option_list( $p_value = null, $p_name = "user_option_list", $p_id = "", $p_attributes = "" ) {
		if($p_id == "") $p_id = $p_name;
		
		echo '<select name="'.$p_name.'" id="'.$p_id.'" '.$p_attributes.'>';
			
			$t_query = "SELECT p.id AS id, p.nome AS nome, p.sobrenome AS sobrenome
						FROM tb_usuario p
						INNER JOIN tb_login u ON p.id = u.usuario_id;";
			$t_row = datab_query($t_query);
			
			foreach( $t_row as $row ) {
				if( $row['id'] == $p_value ) {
					echo '<option selected value="'.$row['id'].'">'.$row['nome'].' '.$row['sobrenome'].'</option>';
				} else {
					echo '<option value="'.$row['id'].'">'.$row['nome'].' '.$row['sobrenome'].'</option>';	
				}
			}
					
		echo '</select>';
	}
	
	/* ========================================
	 * Misc
	 * ========================================
	 */

	/**
	 * Prints the menu on main page.
	 */
	function print_menu() {
		$t_items = array(); //Array of the defaults key for each menu item
		# Prepare submenus
		$t_update_array = array();
		array_push($t_update_array, "people");
		array_push($t_update_array, "user");
		
		# Prepare menus
		$t_items['home_page'] = null;
		if(auth_compare_access(auth_get_current_user_id(), array(ADMINISTRATOR))) { $t_items['subscribe_page'] = null; }
		if(auth_compare_access(auth_get_current_user_id(), array(ADMINISTRATOR, EMPLOYEE))) { $t_items['update_page'] = $t_update_array; }
		$t_items['logout_page'] = null;
		
		# Prints menu
		echo '<div class="menu">';
		echo '<ul id="menu" >';
		
		foreach ( $t_items as $key=>$val ) { //First level items
			if($key == 'logout_page') {
				echo '<li style="padding: 0 5 0 5px; font-weight: bold;"><a href="?page='.$key.' ">'. lang_get($key). '</a>';
			} else {
				echo '<li><a href="?page='.$key.' ">'. lang_get($key). '</a>';
			}
			
			if(is_array($val)) {
//				PRINT '<table><tr><td>';
				echo '<ul>';
				foreach ( $val as $value ) { //Second level items
					echo '<li><a href="?page='.$key.'&type_option_list='. $value .' ">'. lang_get($value). '</a></li>';
				}
				echo '</ul>';
//				PRINT '</td></tr></table>';
			}
			
			echo '</li>';
		}
		# User information
		$t_name = $_SESSION['USER'];
			$t_name = preg_replace('/[^A-Za-z\.]/','$1', $t_name);
			$t_name = strtolower($t_name);
		$t_access = $_SESSION['ACCESS'];
		
		echo '<li title="'.lang_get('username').'" class="session-info1">'.$t_name.'</li>';
		echo '<li title="'.lang_get('access_level').'"class="session-info2">'.lang_get($t_access).'</li>';
		echo '</ul>';
		
		echo '</div>';

	}	
?>